package com.haidong.component;

import com.haidong.Manager;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
 * 用户管理界面
 */
public class MyUserPanel extends JPanel {
    public MyUserPanel(String userClass,int isAdmin) {
        super(new GridLayout(1,3));
        JPanel upper = new JPanel(new GridLayout(12,1));
        JLabel label1 = new JLabel("请输入" + userClass + "账户名:");
        JLabel label2 = new JLabel("请输入" + userClass + "密码:");
        label1.setFont(new Font("Dialog",0,20));
        label2.setFont(new Font("Dialog",0,20));
        TextField textField1 = new TextField();
        TextField textField2 = new TextField();
        textField1.setFont(new Font("Dialog",0,25));
        textField2.setFont(new Font("Dialog",0,25));
        JPanel panel1 = new JPanel(new GridLayout(1,2));
        JPanel panel2 = new JPanel(new GridLayout(1,2));
        panel1.add(label1);
        panel1.add(textField1);
        panel2.add(label2);
        panel2.add(textField2);
        JButton button1 = new JButton("创建" + userClass);
        button1.setFont(new Font("Dialog",0,25));
        JLabel label = new JLabel("");
        label.setFont(new Font("Dialog",0,25));
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    Manager.addUserToDataBase(textField1.getText(),textField2.getText(),isAdmin);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                label.setText("添加" + userClass + "成功!");
            }
        });
        upper.add(new JLabel(""));
        upper.add(new JLabel(""));
        upper.add(panel1);
        upper.add(new JLabel(""));
        upper.add(new JLabel(""));
        upper.add(panel2);
        upper.add(new JLabel(""));
        upper.add(new JLabel(""));
        upper.add(button1);
        upper.add(new JLabel(""));
        upper.add(new JLabel(""));
        upper.add(label);
        add(new JLabel(""));
        add(upper);
        add(new JLabel(""));
    }
    public static JPanel addAdmin(){
        return new MyUserPanel("管理员",1);
    }
    public static MyUserPanel addUser(){
        return new MyUserPanel("用户",0);
    }
    public static JPanel deleteUser(){
        JPanel panel = new JPanel(new GridLayout(1,3));
        JPanel upper = new JPanel(new GridLayout(12,1));
        JLabel label1 = new JLabel("请输入账户名:");
        JLabel label2 = new JLabel("请输入密码:");
        label1.setFont(new Font("Dialog",0,20));
        label2.setFont(new Font("Dialog",0,20));
        TextField textField1 = new TextField();
        TextField textField2 = new TextField();
        textField1.setFont(new Font("Dialog",0,25));
        textField2.setFont(new Font("Dialog",0,25));
        JPanel panel1 = new JPanel(new GridLayout(1,2));
        JPanel panel2 = new JPanel(new GridLayout(1,2));
        panel1.add(label1);
        panel1.add(textField1);
        panel2.add(label2);
        panel2.add(textField2);
        JButton button1 = new JButton("删除");
        button1.setFont(new Font("Dialog",0,25));
        JLabel label = new JLabel("");
        label.setFont(new Font("Dialog",0,25));
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    Manager.deleteUser(textField1.getText(),textField2.getText());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                label.setText("删除成功!");
            }
        });
        upper.add(new JLabel(""));
        upper.add(new JLabel(""));
        upper.add(panel1);
        upper.add(new JLabel(""));
        upper.add(new JLabel(""));
        upper.add(panel2);
        upper.add(new JLabel(""));
        upper.add(new JLabel(""));
        upper.add(button1);
        upper.add(new JLabel(""));
        upper.add(new JLabel(""));
        upper.add(label);
        panel.add(new JLabel(""));
        panel.add(upper);
        panel.add(new JLabel(""));
        return panel;
    }
    public static JPanel updateUser(int userId){
        JPanel panel = new JPanel(new GridLayout(1,3));
        JPanel upper = new JPanel(new GridLayout(12,1));
        JLabel label1 = new JLabel("请输入新密码:");
        JLabel label2 = new JLabel("请确认密码:");
        label1.setFont(new Font("Dialog",0,20));
        label2.setFont(new Font("Dialog",0,20));
        TextField textField1 = new TextField();
        TextField textField2 = new TextField();
        textField1.setFont(new Font("Dialog",0,25));
        textField2.setFont(new Font("Dialog",0,25));
        JPanel panel1 = new JPanel(new GridLayout(1,2));
        JPanel panel2 = new JPanel(new GridLayout(1,2));
        panel1.add(label1);
        panel1.add(textField1);
        panel2.add(label2);
        panel2.add(textField2);
        JButton button1 = new JButton("修改");
        button1.setFont(new Font("Dialog",0,25));
        JLabel label = new JLabel("");
        label.setFont(new Font("Dialog",0,25));
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String psw1 = textField1.getText(),psw2 = textField2.getText();
                    if(psw1.equals(psw2)){
                        Manager.updateUser(userId,psw1);
                        label.setText("修改成功!");
                    }else{
                        label.setText("修改失败!");
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        upper.add(new JLabel(""));
        upper.add(new JLabel(""));
        upper.add(panel1);
        upper.add(new JLabel(""));
        upper.add(new JLabel(""));
        upper.add(panel2);
        upper.add(new JLabel(""));
        upper.add(new JLabel(""));
        upper.add(button1);
        upper.add(new JLabel(""));
        upper.add(new JLabel(""));
        upper.add(label);
        panel.add(new JLabel(""));
        panel.add(upper);
        panel.add(new JLabel(""));
        return panel;
    }
}
